package com.ss.mq.pattern.simple;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DeliverCallback;
import com.ss.mq.util.RabbitUtils;

import java.nio.charset.StandardCharsets;

/**
 * @author 叁昇
 * @create 2020-12-10-18:39
 */
public class SimpleConsumer {
    public static void main(String[] args) throws Exception {
        Connection connection = RabbitUtils.getConnection();
        Channel channel = connection.createChannel();

        // 声明队列, 如果该队列已经创建过, 则不会重复创建
        channel.queueDeclare("SimpleQueue", false, false, false, null);

        // 收到消息后用来处理消息的回调对象
        DeliverCallback callback = (s, delivery) -> {
            String msg = new String(delivery.getBody(), StandardCharsets.UTF_8);
            System.out.println("收到: "+msg);
        };

        // 消费者取消时的回调对象
        CancelCallback cancel = s -> {
        };

        channel.basicConsume("SimpleQueue", true, callback, cancel);
    }
}
